/*
Copyright [2020] [https://www.xiaonuo.vip]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：

1.请不要删除和修改根目录下的LICENSE文件。
2.请不要删除和修改Snowy源码头部的版权声明。
3.请保留源码和相关描述文件的项目出处，作者声明等。
4.分发源码时候，请注明软件出处 https://gitee.com/xiaonuobase/snowy
5.在修改包名，模块名称，项目代码等时，请注明软件出处 https://gitee.com/xiaonuobase/snowy
6.若您的项目无法满足以上几点，可申请商业授权，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.modular.screendetail.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.applet.dto.QuestionnaireDto;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.core.factory.PageFactory;
import vip.xiaonuo.core.pojo.page.PageResult;
import vip.xiaonuo.core.util.PoiUtil;
import vip.xiaonuo.modular.questionnaire.entity.Questionnaire;
import vip.xiaonuo.modular.questionnaire.enums.QuestionnaireExceptionEnum;
import vip.xiaonuo.modular.questionnaire.service.QuestionnaireService;
import vip.xiaonuo.modular.qusstionnaire.entity.Qusstionnaire;
import vip.xiaonuo.modular.qusstionnaire.mapper.QusstionnaireMapper;
import vip.xiaonuo.modular.screen.entity.Screen;
import vip.xiaonuo.modular.screen.enums.ScreenExceptionEnum;
import vip.xiaonuo.modular.screen.service.ScreenService;
import vip.xiaonuo.modular.screendetail.entity.ScreenDetail;
import vip.xiaonuo.modular.screendetail.enums.ScreenDetailExceptionEnum;
import vip.xiaonuo.modular.screendetail.mapper.ScreenDetailMapper;
import vip.xiaonuo.modular.screendetail.param.ScreenDetailParam;
import vip.xiaonuo.modular.screendetail.service.ScreenDetailService;

import java.util.List;

/**
 * 甄选详情表service接口实现类
 *
 * @author blueberry
 * @date 2021-12-05 16:40:47
 */
@Service
public class ScreenDetailServiceImpl extends ServiceImpl<ScreenDetailMapper, ScreenDetail> implements ScreenDetailService {

    @Autowired
    private ScreenService screenService;

    @Autowired
    private ScreenDetailMapper screenDetailMapper;

    @Autowired
    private QuestionnaireService questionnaireService;

    @Autowired
    private QusstionnaireMapper qusstionnaireMapper;

    @Override
    public PageResult<ScreenDetail> page(ScreenDetailParam screenDetailParam) {
        QueryWrapper<ScreenDetail> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(screenDetailParam)) {

            // 根据 查询
            if (ObjectUtil.isNotEmpty(screenDetailParam.getMainId())) {
                queryWrapper.lambda().eq(ScreenDetail::getMainId, screenDetailParam.getMainId());
            }
            // 根据标题 查询
            if (ObjectUtil.isNotEmpty(screenDetailParam.getTitle())) {
                queryWrapper.lambda().eq(ScreenDetail::getTitle, screenDetailParam.getTitle());
            }
            // 根据答题方式 查询
            if (ObjectUtil.isNotEmpty(screenDetailParam.getAnswerMethod())) {
                queryWrapper.lambda().eq(ScreenDetail::getAnswerMethod, screenDetailParam.getAnswerMethod());
            }
            // 根据选择 查询
            if (ObjectUtil.isNotEmpty(screenDetailParam.getQuestTitle())) {
                queryWrapper.lambda().eq(ScreenDetail::getQuestTitle, screenDetailParam.getQuestTitle());
            }
            // 根据答案 查询
            if (ObjectUtil.isNotEmpty(screenDetailParam.getAnswer())) {
                queryWrapper.lambda().eq(ScreenDetail::getAnswer, screenDetailParam.getAnswer());
            }
        }
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<ScreenDetail> list(ScreenDetailParam screenDetailParam) {
        QueryWrapper<ScreenDetail> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(screenDetailParam)) {
            // 根据 查询
            if (ObjectUtil.isNotEmpty(screenDetailParam.getMainId())) {
                queryWrapper.lambda().eq(ScreenDetail::getMainId, screenDetailParam.getMainId());
            }
            // 根据标题 查询
            if (ObjectUtil.isNotEmpty(screenDetailParam.getTitle())) {
                queryWrapper.lambda().like(ScreenDetail::getTitle, screenDetailParam.getTitle());
            }
            // 根据答题方式 查询
            if (ObjectUtil.isNotEmpty(screenDetailParam.getAnswerMethod())) {
                queryWrapper.lambda().like(ScreenDetail::getAnswerMethod, screenDetailParam.getAnswerMethod());
            }
            // 根据选择 查询
            if (ObjectUtil.isNotEmpty(screenDetailParam.getQuestTitle())) {
                queryWrapper.lambda().like(ScreenDetail::getQuestTitle, screenDetailParam.getQuestTitle());
            }
            // 根据答案 查询
            if (ObjectUtil.isNotEmpty(screenDetailParam.getAnswer())) {
                queryWrapper.lambda().like(ScreenDetail::getAnswer, screenDetailParam.getAnswer());
            }
        }
        Questionnaire questionnaire = questionnaireService.getById(screenDetailParam.getWjQuestionnaireId());
        if (ObjectUtil.isNull(questionnaire)){
            throw new ServiceException(QuestionnaireExceptionEnum.NOT_EXIST);
        }
        if (questionnaire.getOther() != 1){
            return null;
        }
        Screen screen = screenService.getByWjQuestionnaireId(screenDetailParam.getWjQuestionnaireId());
        if (ObjectUtil.isNull(screen)){
            throw new ServiceException(ScreenExceptionEnum.NOT_EXIST);
        }

        return screenDetailMapper.findByScreenId(screen.getId());
    }

    @Override
    public void add(ScreenDetailParam screenDetailParam) {
        ScreenDetail screenDetail = new ScreenDetail();
        BeanUtil.copyProperties(screenDetailParam, screenDetail);
        this.save(screenDetail);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<ScreenDetailParam> screenDetailParamList) {
        screenDetailParamList.forEach(screenDetailParam -> {
            this.removeById(screenDetailParam.getId());
        });
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(ScreenDetailParam screenDetailParam) {
        ScreenDetail screenDetail = this.queryScreenDetail(screenDetailParam);
        BeanUtil.copyProperties(screenDetailParam, screenDetail);
        this.updateById(screenDetail);
    }

    @Override
    public ScreenDetail detail(ScreenDetailParam screenDetailParam) {
        return this.queryScreenDetail(screenDetailParam);
    }

    /**
     * 获取甄选详情表
     *
     * @author blueberry
     * @date 2021-12-05 16:40:47
     */
    private ScreenDetail queryScreenDetail(ScreenDetailParam screenDetailParam) {
        ScreenDetail screenDetail = this.getById(screenDetailParam.getId());
        if (ObjectUtil.isNull(screenDetail)) {
            throw new ServiceException(ScreenDetailExceptionEnum.NOT_EXIST);
        }
        return screenDetail;
    }

    @Override
    public void export(ScreenDetailParam screenDetailParam) {
        List<ScreenDetail> list = this.list(screenDetailParam);
        PoiUtil.exportExcelWithStream("SnowyScreenDetail.xls", ScreenDetail.class, list);
    }


    @Override
    public List<ScreenDetail> getByMain(Long id) {
        return screenDetailMapper.findByScreenId(id);
    }

    /**
     * 生成甄别问卷单题
     * @param questionnaireDto
     * @return
     */
    @Override
    public PageResult<ScreenDetail> screenQuestionGenerate(QuestionnaireDto questionnaireDto) {
        Page<ScreenDetail> page = new Page<>();
        page.setCurrent(questionnaireDto.getQuestionNo());
        page.setSize(1);
        Page<ScreenDetail> data = this.page(page);
        PageResult pageResult = new PageResult(data);
        return pageResult;
    }
}
